// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/exec/aggregate_function_registry.h>

#include <pollux/exec/aggregate.h>
#include <pollux/expression/function_signature.h>
#include <pollux/expression/signature_binder.h>
#include <pollux/type/type.h>

namespace kumo::pollux::exec {
    std::pair<TypePtr, TypePtr> resolveAggregateFunction(
        const std::string &functionName,
        const std::vector<TypePtr> &argTypes) {
        if (auto aggregateFunctionSignatures =
                getAggregateFunctionSignatures(functionName)) {
            for (const auto &signature: aggregateFunctionSignatures.value()) {
                SignatureBinder binder(*signature, argTypes);
                if (binder.tryBind()) {
                    return std::make_pair(
                        binder.tryResolveReturnType(),
                        binder.tryResolveType(signature->intermediateType()));
                }
            }
        }

        return std::make_pair(nullptr, nullptr);
    }
} // namespace kumo::pollux::exec
